import 'dart:async';
import 'dart:convert';

import 'package:eplorex/models/metric_item_model.dart';
import 'package:eplorex/providers/user_context.dart';

import '../utils/HttpUtil.dart';

class LocationDao {
  static Future<num?> createPath(String? pathName) async {
    final response = await HttpUtil.getInstance().post(
        "/api/lbs/v1/create/path",
        data: jsonEncode(
            {'userId': UserContext.cxt()?.uid, 'pathName': pathName ?? "户外"}));
    if (response!.statusCode == 200) {
      return response.data['data'];
    } else {
      print("失败");
    }
  }

  static Future<void> reportLocation(Map<String, Object> result, num pathId,
      {bool lastLoc = false}) async {
    final response =
        await HttpUtil.getInstance().post("/api/lbs/v1/report/location",
            data: jsonEncode({
              'userId': UserContext.cxt()?.uid,
              'pathId': pathId,
              'lastLoc': lastLoc,
              'locDates': [result]
            }));
    if (response!.statusCode == 200) {
      print("成功");
    } else {
      print("失败");
    }
  }

  static Future<List<MetricItem>> querySummary(num pathId) async {
    final response = await HttpUtil.getInstance()
        .get("/api/lbs/v1/get/path/analysis", data: {
      'userId': UserContext.cxt()?.uid,
      'pathId': pathId,
    });
    if (response!.statusCode == 200 && response.data != null) {
      return (response.data['data'] as List)
          .map((e) => MetricItem.fromJson(e))
          .toList();
    } else {
      print("失败");
    }
    return [];
  }
}
